package lowijs.XfireClient.packet;

import lowijs.XfireClient.*;
import lowijs.XfireClient.event.*;

public class FriendStatusPacket extends Packet {
	public FriendStatusPacket(byte[] data) {
		int numSids = fromLowHigh(data[12], data[13]);
		Friend[] friends = new Friend[numSids];
		int pos = 14;
		
		FriendManager fm = FriendManager.getInstance();
		
		for (int i = 0; i < numSids; i++) {
			byte[] userid = new byte[4];
			System.arraycopy(data, pos, userid, 0, 4);
			friends[i] = fm.getFriendByUserid(userid);
			pos += 4;
		}
		
		pos += 8; // jump to the sids
		
		for (int i = 0; i < numSids; i++) {
			byte[] sid = new byte[16];
			System.arraycopy(data, pos, sid, 0, 16);
			if (friends[i] != null) {
				if (isZero(sid))
					friends[i].setOffline();
				else {
					friends[i].setSid(sid);
					friends[i].setStatusText("Online");
				}
				EventManager.fireEvent(new FriendStatusEvent(friends[i]));
			}
			pos += 16;
		}
	}
}
